package com.tzinfo.tzt.activity.util;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.tzinfo.tzt.R;
import com.tzinfo.tzt.activity.Car4SModeActivity;
import com.tzinfo.tzt.activity.GroupPurchaseActivity;
import com.tzinfo.tzt.util.Resultmap;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by Administrator on 2014/11/7.
 */
public class FragmentHomeListviewAdapter extends BaseAdapter{

    private Context context;
    private List<Resultmap> data;
    ImageLoader image_loader;
    int ismap;
    double centerx;
    double centery;

    public FragmentHomeListviewAdapter(Context context,List<Resultmap> data) {
        this.context = context;
        this.data = data;
        image_loader = ImageLoader.getInstance();


    }
    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {

        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return data.get(position).getId();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.fragment_item_car_navigation,parent,false);
            holder.nameTextView = (TextView)convertView.findViewById(R.id.item_car_navigation_name);
            holder.brandTextView = (TextView)convertView.findViewById(R.id.item_car_navigation_brand);
            holder.addressTextView = (TextView)convertView.findViewById(R.id.item_car_navigation_address);
            holder.tellTextView = (TextView)convertView.findViewById(R.id.item_car_navigation_tell);
            holder.ll_gotomap =(LinearLayout)convertView.findViewById(R.id.item_car_navigation_gotomap);
            holder.ll_group_purchase =(LinearLayout)convertView.findViewById(R.id.item_car_navigation_group_purchase);
            holder.imageView =(ImageView)convertView.findViewById(R.id.item_car_information_imageView);
            convertView.setTag(holder);
        } else {
            holder =(ViewHolder)convertView.getTag();
        }


        holder.nameTextView.setText(data.get(position).getName().toString());



        holder.nameTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, Car4SModeActivity.class);
                Bundle bundle = new Bundle();
                bundle.putDouble("centerx", data.get(position).getMapinfo().getCenterx());
                bundle.putDouble("centery", data.get(position).getMapinfo().getCentery());
                bundle.putString("Id", String.valueOf(data.get(position).getId()));
                bundle.putString("image_url",data.get(position).getLogo().toString());
                intent.putExtras(bundle);
                context.startActivity(intent);
                //              Toast.makeText(context,""+data.get(position).getId(),Toast.LENGTH_SHORT).show();
            }
        });
        holder.brandTextView.setText((String)(data.get(position).getBrand()));
        holder.addressTextView.setText((String)(data.get(position).getAddress()));
        holder.tellTextView.setText("电话:"+ data.get(position).getTell());
        holder.ll_gotomap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    ismap = data.get(position).getMapinfo().getIsmap();
                    centerx  = data.get(position).getMapinfo().getCenterx();
                    centery  = data.get(position).getMapinfo().getCentery();

                Uri location = Uri.parse("geo:"+centery+","+centerx+"?q="+data.get(position).getName().toString());
                Intent mapIntent = new Intent(Intent.ACTION_VIEW, location);

// Verify it resolves
                PackageManager packageManager = context.getPackageManager();
                List<ResolveInfo> activities = packageManager.queryIntentActivities(mapIntent, 0);
                boolean isIntentSafe = activities.size() > 0;

// Start an activity if it's safe
                if (isIntentSafe) {
                    context.startActivity(mapIntent);
                }
            }
        });

        holder.ll_group_purchase.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, GroupPurchaseActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("Id", String.valueOf(data.get(position).getId()));
                bundle.putString("image_url",data.get(position).getLogo().toString());
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });

        image_loader.displayImage(data.get(position).getLogo(),holder.imageView);
        return  convertView;
    }

    class ViewHolder{
        LinearLayout ll_group_purchase;
        TextView nameTextView;
        TextView brandTextView;
        TextView addressTextView ;
        TextView tellTextView ;
        LinearLayout ll_gotomap ;
        ImageView imageView;
    }
}
